/*
 * searching.h
 *
 *  Created on: 11-dec.-2013
 *      Author: Axel
 */

#ifndef SEARCHING_H_
#define SEARCHING_H_

namespace search {

template <class T>
int binarysearch(T x, T* list, int current) {
	int links = 0, rechts = current;
	int midden = (links + rechts)/2;
	bool found = false;

	while (links<=rechts && !found) {
		found = list[midden] == x;

		if (list[midden] > x)
				rechts = midden - 1;

		else if (list[midden] < x)
				links = midden + 1;

		midden = (links + rechts)/2;
	}

	if (found)
		return (midden);
	else
		return (-1);
}

template <class T>
int lineairsearch(T x, int current, T* list) {
	int start = 0;
	bool found = false;

	while (!found && start<=current) {
		found = list[start] == x;
		if (!found)
			start++;
	}

	if (found)
		return (start);
	else
		return (-1);
}

}

#endif /* SEARCHING_H_ */
